package com.controller.crud;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.lang.StringUtils;

import com.splwg.base.api.datatypes.DateTime;
import com.splwg.base.api.service.DataElement;
import com.splwg.base.api.service.PageHeader;
import com.splwg.cmun.common.JdbcCrudTools;
import com.splwg.shared.common.ApplicationError;
import com.splwg.shared.common.LoggedException;
import com.splwg.shared.logging.Logger;
import com.splwg.shared.logging.LoggerFactory;



/**
*
@QueryPage (program = UNCIIT002TASK_CRUD, service = UNCIIT002TASK_CRUD, modules = {},
    header = {@DataField (name = UN_CII_002_MAIN_ID)},    headerFields = {@DataField (name = UN_CII_002_MAIN_ID)},
    body = @DataElement (contents = {@DataField (name = UN_CII_002_MAIN_ID)
            , @DataField (name = UN_CII_002_TASK_NAME)
            , @DataField (name = UN_CII_002_INFO)
            , @DataField (name = UN_CII_002_STATUS)
            , @DataField (name = UN_CII_002_COUTENT)
            , @DataField (name = UN_CII_002_START_TIME)
            , @DataField (name = UN_CII_002_END_TIME)
            }),
    actions = { "change" , "add" , "read", "delete"})
*/
public class UnCiiT002TaskCRUDController extends UnCiiT002TaskCRUDController_Gen {
    private static final Logger logger = LoggerFactory.getLogger(UnCiiT002TaskCRUDController.class);
    private String tableName = "UN_CII_T_002_TASK";

    @Override
    protected void change(DataElement item) {
        logger.info("=============");
        logger.info("UN_CII_002_MAIN_ID:"+item.get(STRUCTURE.UN_CII_002_MAIN_ID));
        logger.info("UN_CII_002_TASK_NAME:"+item.get(STRUCTURE.UN_CII_002_TASK_NAME));
        logger.info("UN_CII_002_INFO:"+item.get(STRUCTURE.UN_CII_002_INFO));
        logger.info("UN_CII_002_STATUS:"+item.get(STRUCTURE.UN_CII_002_STATUS));
        logger.info("UN_CII_002_COUTENT:"+item.get(STRUCTURE.UN_CII_002_COUTENT));
        logger.info("UN_CII_002_START_TIME:"+item.get(STRUCTURE.UN_CII_002_START_TIME));
        logger.info("UN_CII_002_END_TIME:"+item.get(STRUCTURE.UN_CII_002_END_TIME));
        logger.info("=============");

        Connection connection = JdbcCrudTools.getNewConnection();
        PreparedStatement statement = null;
        String sql = "UPDATE " + tableName  +" SET ";
        sql += "   UN_CII_002_TASK_NAME = ? ,  UN_CII_002_INFO = ? ,  UN_CII_002_STATUS = ? ,  UN_CII_002_COUTENT = ? ,  UN_CII_002_START_TIME = ? ,  UN_CII_002_END_TIME = ? ,";
        sql = sql.substring(0, sql.length() - 1);
        sql += " WHERE   UN_CII_002_MAIN_ID = ?       ";
        try {
            connection.setAutoCommit(false);
            statement = connection.prepareStatement(sql.toString());
            int index = 1;
                            statement.setString(index++, item.get(STRUCTURE.UN_CII_002_TASK_NAME));
                            statement.setString(index++, item.get(STRUCTURE.UN_CII_002_INFO));
                            statement.setString(index++, item.get(STRUCTURE.UN_CII_002_STATUS));
                            statement.setString(index++, item.get(STRUCTURE.UN_CII_002_COUTENT));
                            statement.setString(index++, item.get(STRUCTURE.UN_CII_002_START_TIME));
                            statement.setString(index++, item.get(STRUCTURE.UN_CII_002_END_TIME));
        
                statement.setString(index++, item.get(STRUCTURE.UN_CII_002_MAIN_ID));
                                        //DateTime.bindToSQL(statement, 5, item.get(STRUCTURE.UN_CII_003_START_TIME));
            statement.executeUpdate();
            connection.commit();
        } catch (SQLException e) {
            throw LoggedException.wrap(logger, "Failed execution of the SQL prepared statement " + sql
            + " bound to values "+tableName+"Unexpected SQLException", e);
        } finally {
            JdbcCrudTools.close(connection, statement);
            this.setOverrideResultForChange(item);
        }
    }


    @Override
    protected DataElement read(PageHeader header) throws ApplicationError {
        DataElement element = new DataElement();
        logger.info("读取进入了---------------------------");
        logger.info("come in " + this.getClass().getName() + " read Method");
    
        String guId = header.getString(STRUCTURE.HEADER.UN_CII_002_MAIN_ID);
        if(StringUtils.isEmpty(guId)){
            logger.info("UN_CII_002_MAIN_ID can not be empty");
            return element;
        }
        String sql = "SELECT * from " + tableName  +" WHERE UN_CII_002_MAIN_ID = ?";
        Connection connection = JdbcCrudTools.getNewConnection();
        PreparedStatement statement = null;
        try {
            statement = connection.prepareStatement(sql);
            statement.setString(1, guId);
            ResultSet result = statement.executeQuery();
            while (result.next()){
                element = new DataElement();

                        element.put(STRUCTURE.UN_CII_002_MAIN_ID , result.getString("UN_CII_002_MAIN_ID"));
                        element.put(STRUCTURE.UN_CII_002_TASK_NAME , result.getString("UN_CII_002_TASK_NAME"));
                        element.put(STRUCTURE.UN_CII_002_INFO , result.getString("UN_CII_002_INFO"));
                        element.put(STRUCTURE.UN_CII_002_STATUS , result.getString("UN_CII_002_STATUS"));
                        element.put(STRUCTURE.UN_CII_002_COUTENT , result.getString("UN_CII_002_COUTENT"));
                        element.put(STRUCTURE.UN_CII_002_START_TIME , result.getString("UN_CII_002_START_TIME"));
                        element.put(STRUCTURE.UN_CII_002_END_TIME , result.getString("UN_CII_002_END_TIME"));
                        //element.put(STRUCTURE.UN_CII_003_END_TIME , DateTime.fromResultSet(result, "UN_CII_003_END_TIME"));
            }
            logger.info(element);
        } catch (Exception e) {
            throw LoggedException.wrap(logger, "Failed execution of the SQL prepared statement " + sql
            + " bound to values " + tableName + " Unexpected SQLException", e);
        } finally {
            JdbcCrudTools.close(connection,statement);
        }
                            
        logger.info("读取结束了---------------------------");
        return element;
    }


     protected void delete(DataElement item) {
    
        logger.info("=============");
        logger.info("UN_CII_002_MAIN_ID:"+item.get(STRUCTURE.UN_CII_002_MAIN_ID));
        logger.info("=============");
        String guId = item.get(STRUCTURE.UN_CII_002_MAIN_ID);
        if(StringUtils.isEmpty(guId)){
            logger.info("UN_CII_002_MAIN_ID can not be empty");
        }
        String sql = "DELETE from " + tableName  +" WHERE UN_CII_002_MAIN_ID  = ?";
        Connection connection = JdbcCrudTools.getNewConnection();
        PreparedStatement statement = null;
        try {
            statement = connection.prepareStatement(sql);
            statement.setString(1, guId);
            ResultSet result = statement.executeQuery();
            logger.info(result);
        } catch (Exception e) {
            throw LoggedException.wrap(logger, "Failed execution of the SQL prepared statement " + sql
            + " bound to values " + tableName + " Unexpected SQLException", e);
        } finally {
            JdbcCrudTools.close(connection,statement);
            this.setOverrideResultForChange(item);
        }
                                }
    @Override
    protected PageHeader add(DataElement item) throws ApplicationError {
        logger.info("=============");
                logger.info("UN_CII_002_MAIN_ID:"+item.get(STRUCTURE.UN_CII_002_MAIN_ID));
                logger.info("UN_CII_002_TASK_NAME:"+item.get(STRUCTURE.UN_CII_002_TASK_NAME));
                logger.info("UN_CII_002_INFO:"+item.get(STRUCTURE.UN_CII_002_INFO));
                logger.info("UN_CII_002_STATUS:"+item.get(STRUCTURE.UN_CII_002_STATUS));
                logger.info("UN_CII_002_COUTENT:"+item.get(STRUCTURE.UN_CII_002_COUTENT));
                logger.info("UN_CII_002_START_TIME:"+item.get(STRUCTURE.UN_CII_002_START_TIME));
                logger.info("UN_CII_002_END_TIME:"+item.get(STRUCTURE.UN_CII_002_END_TIME));
                logger.info("=============");

        Connection connection = JdbcCrudTools.getNewConnection();
        PreparedStatement statement = null;
        String sql = "insert into " + tableName  +" ( ";
        sql += " UN_CII_002_MAIN_ID, UN_CII_002_TASK_NAME, UN_CII_002_INFO, UN_CII_002_STATUS, UN_CII_002_COUTENT, UN_CII_002_START_TIME, UN_CII_002_END_TIME,";
        sql = sql.substring(0, sql.length() - 1);
        sql += ") values( ";
        sql += " ?, ?, ?, ?, ?, ?, ?,";
        sql = sql.substring(0, sql.length() - 1);
        sql += ") ";
        try {
            int index = 1;
            connection.setAutoCommit(false);
            statement = connection.prepareStatement(sql.toString());
                        statement.setString(index++, item.get(STRUCTURE.UN_CII_002_MAIN_ID));
                        statement.setString(index++, item.get(STRUCTURE.UN_CII_002_TASK_NAME));
                        statement.setString(index++, item.get(STRUCTURE.UN_CII_002_INFO));
                        statement.setString(index++, item.get(STRUCTURE.UN_CII_002_STATUS));
                        statement.setString(index++, item.get(STRUCTURE.UN_CII_002_COUTENT));
                        statement.setString(index++, item.get(STRUCTURE.UN_CII_002_START_TIME));
                        statement.setString(index++, item.get(STRUCTURE.UN_CII_002_END_TIME));
                        //DateTime.bindToSQL(statement, 7, item.get(STRUCTURE.UN_CII_003_END_TIME));
            statement.executeUpdate();
            connection.commit();
        } catch (SQLException e) {
            throw LoggedException.wrap(logger, "Failed execution of the SQL prepared statement " + sql
            + " bound to values "+tableName+" Unexpected SQLException", e);
        } finally {
            JdbcCrudTools.close(connection, statement);
            this.setOverrideResultForChange(item);
        }
        return super.add(item);
    }
}
